package com.and9.mchen.mcmusic.util;

import java.util.Iterator;

import com.and9.mchen.mcmusic.entity.Album;
import com.and9.mchen.mcmusic.entity.ImageUrl;
import com.and9.mchen.mcmusic.entity.MusicStyle;
import com.and9.mchen.mcmusic.entity.Singer;
import com.and9.mchen.mcmusic.entity.Song;
import com.and9.mchen.mcmusic.entity.Statistics;
import com.and9.mchen.mcmusic.vo.OmitAlbumBean;
import com.and9.mchen.mcmusic.vo.OmitMusicStyleBean;
import com.and9.mchen.mcmusic.vo.OmitSongBean;

public final class ObjectMapping {

	private ObjectMapping(){}
	
	public static OmitSongBean omitSongBeanMappingBySong(Song song){
		
		if(song == null){
			return null;
		}
		
		OmitSongBean.Builder builder=new OmitSongBean.Builder();
		
		builder=builder.songId(song.getId()).songUniqueId(song.getUniqueId()).
			songName(song.getSongName()).songUrl(song.getSongUrl()).
				songLength(song.getSongLength());
		
		if(song.getSinger() != null){
			Singer singer=song.getSinger();
			builder=builder.singerId(singer.getId()).singerUniqueId(singer.getUniqueId()).
					singerName(singer.getName());
		}
		
		
		if(!GEntityHelper.isEmpty(song.getOtherSingers())){
			
			StringBuilder stringBuilder=new StringBuilder();
			Iterator<Singer> iterator=song.getOtherSingers().iterator();
			stringBuilder.append("(feat."+iterator.next().getName());
			while(iterator.hasNext()){
				stringBuilder.append(", "+iterator.next().getName());
			}
			stringBuilder.append(")");
			builder=builder.featWith(stringBuilder.toString());
		}
		
		if(song.getStatistics() != null ){
			
			Statistics statistics=song.getStatistics();
			
			builder=builder.isStick(statistics.getIsStick()).
				supprotCount(statistics.getSupprotCount()).
					nonSupportCount(statistics.getNonSupportCount()).
						clickCount(statistics.getClickCount());
		}
		
		return builder.build();
	}
	
	public static OmitAlbumBean omitAlbumBeanMappingByAlbum(Album album){
		
		if(album == null){
			return null;
		}
		
		OmitAlbumBean.Builder builder=new OmitAlbumBean.Builder();
		
		builder=builder.albumId(album.getId()).albumUniqueId(album.getUniqueId()).
			albumName(album.getAlbumName());
		
		if(album.getSinger() != null){
			Singer singer=album.getSinger();
			builder=builder.singerId(singer.getId()).singerUniqueId(singer.getUniqueId()).
					singerName(singer.getName());
		}
		
		if(album.getStatistics() != null){
			Statistics statistics=album.getStatistics();
			builder=builder.statisticsId(statistics.getId()).statisticsUniqueId(statistics.getUniqueId()).
					isStick(statistics.getIsStick()).supprotCount(statistics.getSupprotCount()).
						nonSupportCount(statistics.getNonSupportCount()).
							clickCount(statistics.getClickCount());
		}
		
		if(album.getImageUrl() != null){
			ImageUrl imageUrl=album.getImageUrl();
			builder=builder.imageUrlId(imageUrl.getId()).imageUrlUniqueId(imageUrl.getUniqueId()).
				imageUrl_min(imageUrl.getMin()).imageUrl_mid(imageUrl.getMiddle()).
					imageUrl_max(imageUrl.getMax());
		}
		
		return builder.build();
	}
	
	public static OmitMusicStyleBean omitMusicStyleBeanMappingByMusicStyle(MusicStyle musicStyle){
		
		if(musicStyle == null){
			return null;
		}
		
		OmitMusicStyleBean.Builder builder=new OmitMusicStyleBean.Builder();
		
		builder=builder.musicStyleId(musicStyle.getId()).musicStyleName(musicStyle.getStyleName()).
				musicStyleUniqueId(musicStyle.getUniqueId());
		
		return builder.build();
		
	}
}
